package com.arsn.myf.system.controller;

import com.arsn.myf.common.util.excel.ExcelUtil;
import com.arsn.myf.common.util.result.Func;
import com.arsn.myf.common.util.result.R;
import com.arsn.myf.system.entity.User;
import com.arsn.myf.system.excel.UserExcel;
import com.arsn.myf.system.excel.UserImporter;
import com.arsn.myf.system.service.UserService;
import com.arsn.myf.system.vo.UserVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.wildfly.common.annotation.NotNull;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 用户
 * @author zhongrj
 * @since 2022-06-07
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
@Api(value = "用户", tags = "用户")
public class UserController {

    private final UserService userService;

    private final BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 查询用户的分页数据
     * @param page
     * @param user
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询用户的分页数据")
    public R list(Page<User> page, User user){
        return R.data(userService.page(page,new QueryWrapper<>(user)));
    }


    /**
     * 自定义用户的分页数据
     * @param page
     * @param user
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "自定义用户的分页数据")
    public R page(Page<UserVO> page, UserVO user){
        return R.data(userService.selectPageUserList(page,user));
    }


    /**
     * 用户详情信息
     * @param user
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "用户详情信息")
    public R detail(User user){
        return R.data(userService.getOne(new QueryWrapper<>(user)));
    }


    /**
     * 用户新增
     * @param user
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "用户新增")
    public R save(@RequestBody User user){
        //密码加密处理
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        //新增并返回
        return R.status(userService.insertUser(user));
    }

    /**
     * 用户修改
     * @param user
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "用户修改")
    public R update(@RequestBody User user){
        return R.status(userService.updateUser(user));
    }


    /**
     * 删除,批量删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ApiOperation(value = "批量删除")
    public R remove(@ApiParam("用户ids") @NotNull String ids){
        return R.status(userService.removeByIds(Func.stringToLong(Arrays.asList(ids.split(",")))));
    }

    /**
     * 导出模板
     * @param response
     */
    @GetMapping("/export-template")
    @ApiOperation(value = "导出模板")
    public void exportTemplate(HttpServletResponse response) {
        List<UserExcel> list = new ArrayList<>();
        //设置模板示例数据
        UserExcel excel = new UserExcel();
        excel.setRealName("张三");
        excel.setNickName("zhangsan");
        excel.setDeptId("信息部门");
        excel.setSex("男");
        excel.setEmail("12345678@qq.com");
        excel.setPhone("13112341234");
        list.add(excel);
        //导出
        ExcelUtil.export(response, "用户数据模板", "用户数据表", list, UserExcel.class);
    }

    /**
     * 导出用户数据
     * @param response
     * @param user
     */
    @GetMapping("/export-user")
    @ApiOperation(value = "导出用户数据")
    public void exportUser(HttpServletResponse response,UserVO user) {
        List<UserExcel> list = userService.exportUserExcelPage(user);
        ExcelUtil.export(response, "用户数据导出",  list, UserExcel.class);
    }

    /**
     * 导入用户数据
     * @param file
     * @param isCovered
     */
    @PostMapping("/import-user")
    @ApiOperation(value = "导入用户数据")
    public void importUser(MultipartFile file, boolean isCovered) {
        UserImporter importer = new UserImporter(userService,isCovered);
        ExcelUtil.save(file,importer,UserExcel.class);
    }

    /**
     * 查询所有用户
     * @param user
     * @return
     */
    @GetMapping("/getUserList")
    @ApiOperation(value = "查询所有用户")
    public R getUserList(UserVO user){
        return R.data(userService.getUserList(user));
    }


    /**
     * 用户密码修改
     * @param user(必须包含 id, password,newPassword)
     * @return
     */
    @PostMapping("/updatePassword")
    @ApiOperation(value = "用户密码修改",notes = "传入user(必须包含 id, password,newPassword)")
    public R updatePassword(@RequestBody UserVO user){
        return R.status(userService.updatePassword(user));
    }


}
